
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { regionArea,getProduct,docentCommission,down } from './commissionPaymentListServ.js'
import { makeAddress,download } from "utils/util"

const defultState = {
    biz:{},
    area1:[],
    products:[],
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'regionArea', payload:{}});
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
        }
      })
    },
  },


  effects: {
    *regionArea({ payload },{ put, call, select }){
      let {biz,areaList} = yield select(state => state[tmpModule.namespace])
      const { data, success } = yield call(regionArea);
      if(success) {
        if(biz.queryForm && biz.queryForm.regionId){
          data.map((item,index)=>{
            if(biz.queryForm && biz.queryForm.regionId && biz.queryForm.regionId==item.value)
            {   
              areaList=item.children
            }
          })
        }
        yield put({ type: 'updateStore',payload:{area1:data,areaList}})
      }
    },
    *getProduct({payload},{put,call,select}){
      let {biz,products} = yield select(state => state[tmpModule.namespace])
      const { data, success } = yield call(getProduct,payload);
      if(success){
        products=data
        yield put({ type: 'updateStore',payload:{products}})
      }
    },
    //分佣
    // *centCommission({ payload }, { put, call, select }){
    //   try{
    //     const { success,msg } = yield call(docentCommission,payload.id);
    //     let defultState = yield select(state => state[tmpModule.namespace])
    //     if(success){
    //        message.success(msg,()=>{
    //          defultState.biz.refreshListDatas(true)
    //        })
    //     }else{
    //       message.success(msg)
    //     }
    //  }catch(e){
    //      console.error(e)
    //  }
    // },
    * down ({payload}, {put, call, select}) {
      try {
        let tmpState = yield select(state => state[tmpModule.namespace])
        let {queryForm} = tmpState.biz
        const {data, success} = yield call(down,queryForm)
        yield put({type: 'updateStore', payload: {exportLoading: false}})
        if (success) {
          // yield put({ type: 'updateStore', payload: {  data } });
          let url = data.url || ''
          if (url) {
            download(url)
          }
          message.success('操作成功')
          // window.open(data.url)
          
        }
      } catch (e) {
        console.error(e)
      }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
